Ext.ns('view');
view.view_logo = function(status,id){
	var page_id;
	var edit;
	switch(status){
		case 'logo_home':
			page_id= 1;
		break;
		case 'logo_menu':
			page_id= 2;
		break;
		case 'logo_collection':
			page_id= 3;
		break;
		case 'logo_promotion':
			page_id= 4;
		break;
		case 'logo_contact':
			page_id= 6;
		break;
	}
	Ext.Ajax.request({
		url:  'services/view_logo.php',
		method: 'POST',
		params: {act : status},
		success: function ( result, request ) {	
			var data = result.responseText.trim();
			if (data != "") {
				var location;
				arr =data.split('}#');
				for(var i=0;i<arr.length;i++){
					arr[i]=arr[i].replace('{','');
					arr[i]=arr[i].replace('}','');
					arr[i]=arr[i].split('#');
				}
				var viewtable="	<table class ='view-table' border=1px solid #CCC style='text-align:center;'>" +
							"<th>STT</th><th>Ngày</th><th>Vị&nbsp;Trí</th><th>Hình&nbsp;Ảnh</th><th>Chọn</th>";
	
				for(var i =0 ; i<arr.length ; i++){
					viewtable+= "<tr>";				
					viewtable+= "<td>"+(i+1)+ "</td>";
					viewtable+= "<td>"+ arr[i][2] + "</td>";
					if (arr[i][3] ==1){
						location ="RIGHT";
					}
					else{
						location = "LEFT";
					}
					viewtable+= "<td>"+ location + "</td>";
					viewtable+= "<td><img src='../"+ arr[i][4] +"' height='50' width='50' /></td>";
					viewtable += "<td><input type='checkbox' id='"+status+"' name='"+status+"' value='" + arr[i][0] + "'/></td>";
					viewtable+= "</tr>";
				}
				viewtable +="</table>";
				$('#'+id).html(viewtable);
			}else{
				var viewtable =  "<p style='color: Blue;line-height:15px; top:16px; font-size:20px; font-family:tahoma; text-align:center;'>" + 
				"Không tồn tại dữ liệu</p>";
				$('#'+id).html(viewtable);
			}
		},
		failure: function ( result, request ) {
    	}
	});
}
view.add_image = function(status,event_id){
	Ext.Ajax.request({
		url:  'services/view_image.php',
		method: 'POST',
		params: {act : status,
				 event_id: event_id },
		success: function ( result, request) {
			var data = result.responseText.trim();
			var mp;
			if (data != ""){ 
				arr =data.split('}#');
				for(var i=0;i<arr.length;i++){
					arr[i]=arr[i].replace('{','');
					arr[i]=arr[i].replace('}','');
					arr[i]=arr[i].split('#');
				}
				if(status == 'image_onpress'){
					view.load_images2(status,1);
				}else{
					view.load_images(status,1);
				}
				
			}else{
				var str= "<p style='color: Blue;line-height:15px; top:16px; font-size:15px; font-family:tahoma; text-align:center'>" +
					 "Không tồn tại dữ liệu</p>";
				$('#image').html(str);
			}
		},
		failure: function ( result, request ) {
    	}
	});
}
function innit(status){
	common.setUnCheckAll('image_gallery');
	if($('#btnuncheck').css('display') != 'none'){
		$('#btncheck,#btnuncheck').toggle();
	}
	if($('#btnupdate').css('display') != 'none'){
		$("#btnupdate,#btnedit,#btncancel").toggle();
		$("#btndel,#btncheck,#btnuncheck").removeAttr('disabled');
	}	
}
view.load_images = function (status,index){
	var mp = (index * 10);
	var min = index*10 - 10;
	if( mp > arr.length){
		mp = arr.length;
	}
	innit(status);
	var str = "<table cellpadding=\"3\" cellspacing=\"0\" width=\"100%\" class=\"xpstyle\" border=\"1\" bordercolor=\"#999999\">";
	str += "<th>Id</td><th>Image</th><th>Content</th><th>Thumb</th><th>Index</th><th>Check</th>";
	for(var i = min ; i<mp; i++){
		var filename = 'image_' + arr[i][0];
		str += "<tr align='center' style='height:50px;'>";
		str += "<td style='width:10px;'>" + (i+1) + "</td>";
		str += "<td style='width:50px;'><img id='" + filename + "' name ='" + filename + "' src='../upload/thumb/" + arr[i][4] + "' height='50' width='50' /></td>";
		str += "<td style='width:500px;'><textarea style='width:500px;height:50px;' id='tb_"+ arr[i][0] + "' disabled='disabled'>"+ arr[i][5] + "</textarea></td>";
		if(arr[i][9] == 0 || !arr[i][9]){
			str += "<td style='width:40px;'><span>N/A</span></td>";
		}else{
			str += "<td style='width:40px;'><span style ='color:red;'>Thumbs</span></td>";
		}
		if(arr[i][12]){
			str += "<td style='width:50px;'><input type='textbox' style='width:20px' name='txt_index' id='index_"+ arr[i][0] +"' value='"+ arr[i][12] +"'/></td>";
		}else{
			str += "<td style='width:50px;'><input type='textbox' style='width:25px;height:25px' name='txt_index' id='index_"+ arr[i][0] +"' value=''/></td>";
		}
		
		str += "<td style='width:40px;'><input id='"+ status +"'name= '" + status + "[]' type='checkbox' value='"+ arr[i][0] +"'/></td>";
		
		str += "</tr>";
	}
	str += "</table>";
	var name = status.split("_");
	var num  = parseInt((arr.length - 1)/10) +1;
	str += "<select id='select_" +name[1] +"'>";
	for(var i =0;i<num;i++){
		str += "<option value='" + (i+1) + "'";
		str += (index == (i+1)) ? "selected": ""; 
		str += " >" +  (i+1) + "</option>";
	}
	str += "</select>";
	$('#image').html(str);
	
	$('#select_'+ name[1]).change(function(){
		var idx  = $('#select_'+ name[1]).val();
		view.load_images(status,idx);
	});
}
view.load_images2 = function (status,index){
	var mp = (index * 10);
	var min = index*10 - 10;
	if( mp > arr.length){
		mp = arr.length;
	}

	innit(status);
	var name = status.split("_");
	var str = "<select id='allies_" +name[1] +"'>";
	str +="<option disabled='disabled' value='0'>---</option>";
	for(var i=0; i<arr.length; i++){
		str+= "<option value='"+ arr[i][0] +"'>" + arr[i][0] +"</option>";
	}
	str += "</select>";
	str += "<table cellpadding=\"3\" cellspacing=\"0\" width=\"100%\" class=\"xpstyle\" border=\"1\" bordercolor=\"#999999\">";
	str += "<th>Id</td><th>Image</th><th>Content</th><th>Allies</th><th>Check</th>";
	for(var i = min ; i<mp; i++){
		var filename = 'image_' + arr[i][0];
		str += "<tr align='center' style='height:50px;'>";
		str += "<td style='width:10px;'>" + arr[i][0] + "</td>";
		str += "<td style='width:50px;'><img id='" + filename + "' name ='" + filename + "' src='../upload/thumb/" + arr[i][4] + "' height='50' width='50' /></td>";
		str += "<td style='width:550px;'><textarea style='width:550px;height:50px;' id='tb_"+ arr[i][0] + "' disabled='disabled'>"+ arr[i][5] + "</textarea></td>";
		if(arr[i][10] == 0 || !arr[i][10]){
			str += "<td style='width:40px;'><span>N/A</span></td>";
		}else{
			str += "<td style='width:40px;'><span style ='color:red;'>" + arr[i][10] +"</span></td>";
		}
		str += "<td style='width:40px;'><input id='"+ status +"'name= '" + status + "[]' type='checkbox' value='"+ arr[i][0] +"'/></td>";
		str += "</tr>";
		str +="<input type='hidden' name='image_hidden[]'value ='" + arr[i][4] + "'/>";
	}
	str += "</table>";
	
	var num  = parseInt((arr.length - 1)/10) +1;
	str += "<select id='select_" +name[1] +"'>";
	for(var i =0;i<num;i++){
		str += "<option value='" + (i+1) + "'";
		str += (index == (i+1)) ? "selected": ""; 
		str += " >" +  (i+1) + "</option>";
	}
	str += "</select>";
	$('#image').html(str);

	$('#select_'+ name[1]).change(function(){
		var idx  = $('#select_'+ name[1]).val();
		view.load_images2(status,idx);
	});
}

view.edit_click = function(ids){
	var list_id = ids.split(',');
	$("#btnupdate,#btnedit,#btncancel").toggle();
	$('#btncheck,#btnuncheck,#btndel').attr('disabled',true);
	for(var i=0 ; i< list_id.length;i++){
		$('#tb_' + list_id[i]).removeAttr('disabled');
		$('#tb_' + list_id[i]).css('border','1px solid #B8860B');
	}
}
view.update_click = function(status,ids){
	$("#btnupdate,#btnedit,#btncancel").toggle();
	$("#btndel,#btncheck,#btnuncheck").removeAttr('disabled');
	var list = document.getElementsByName(status);
	$(list).removeAttr('disabled');
	list_id = ids.split(",");
	for(var i=0 ; i< list_id.length;i++){
		$('#tb_' + list_id[i]).attr('disabled',true);
		$('#tb_' + list_id[i]).css('border','');
	}
	common.setUnCheckAll(status);
	if($('#btnuncheck').css('display') != 'none'){
		$('#btncheck,#btnuncheck').toggle();
	}
	var comment = "";
	for(var i=0;i<list_id.length;i++){
		if (i < list_id.length -1){
			comment += $('#tb_' + list_id[i]).val().trim() + "#";
		}else{
			comment += $('#tb_' + list_id[i]).val().trim();
		}
	}
	Ext.Ajax.request({
		url:  'services/add_comment.php',
		method: 'POST',
		params: {act : status,
				 ids : ids,
				 comment : comment},
		success: function ( result, request ) {
			var data = result.responseText.trim();
			view.add_image(status);
		}
	});
}
view.allies_click = function(status,ids){
	Ext.Ajax.request({
		url:  'services/add_allies.php',
		method: 'POST',
		params: {
			index: status,
			ids : ids
		},
		success: function ( result, request ) {
			var data = result.responseText.trim();
			view.add_image('image_onpress');
		}
	});
}
view.view_content = function(status,id){
	var page_id;
	var edit;
	switch(status){
		case 'content_home':
			page_id= 1;
			edit = 'edit_home';
		break;
		case 'content_menu':
			page_id= 2;
			edit = 'edit_menu';
		break;
		case 'content_collection':
			page_id= 3;
			edit = 'edit_collection';
		break;
		case 'content_promotion':
			page_id= 4;
			edit = 'edit_promotion';
		break;
		case 'content_contact':
			page_id= 6;
			edit = 'edit_contact';
		break;
	}
	Ext.Ajax.request({
		url:  'services/view.php',
		method: 'POST',
		params: {act : status},
		success: function ( result, request ) {	
			var data = result.responseText.trim();
			if (data != "") {
				arr = data.split('}#');
				for (var i = 0; i < arr.length; i++) {
					arr[i] = arr[i].replace('{', '');
					arr[i] = arr[i].replace('}', '');
					arr[i] = arr[i].split('#');
				}
				var viewtable = "	<table class ='view-table' border=1px solid #CCC style='text-align:center;'>" +
				"<th>STT</th><th>Ngày</th><th>Nội&nbsp;Dung</th><th>Sửa</th><th>Chọn</th>";
				for (var i = 0; i < arr.length; i++) {
					viewtable += "<tr>";
					viewtable += "<td>" + (i + 1) + "</td>";
					viewtable += "<td>" + arr[i][2] + "</td>";
					viewtable += "<td style='text-align:justify;'>" + arr[i][3] + "</td>";
					viewtable += "<td><a href='?act=edit_content&id=" + arr[i][0] + "&page="+ page_id +"'><img id='"+ edit +"' value='" + arr[i][0] + "'  src='../images/edit.gif' border='0' title=''></a></td>";
					viewtable += "<td><input type='checkbox' id='"+status+"' name='"+status+"' value='" + arr[i][0] + "'/></td>";
					viewtable += "</tr>";
				}
				viewtable += "</table>";
				$('#'+id).html(viewtable);
			}else{
				var viewtable =  "<p style='color: Blue;line-height:15px; top:16px; font-size:20px; font-family:tahoma; text-align:center;'>" + 
				"Không tồn tại dữ liệu</p>";
				$('#'+id).html(viewtable);
			}
		},
		failure: function ( result, request ) {
    	}
	});
}

view.delete_content = function (page_id,ids,id_content){
	var status;
		switch(page_id){
		case '1':
			status = 'content_home';
		break;
		case '2':
			status = 'content_menu';
		break;
		case '3':
			status = 'content_collection';
		break;
		case '4':
			status = 'content_promotion';
		break;
		case '6':
			status = 'content_contact';
		break;
	}
	Ext.Ajax.request({
		url:  'services/content.php',
		method: 'POST',
		params: {act : 'delete_content',
			     ids : ids,
			     page_id: page_id},
		success: function ( result, request ) {	
			if (result.responseText == 1){
				alert("Xóa thành công");
				view.view_content(status, id_content);
			}
		},
		failure: function ( result, request ) {
		}
	});
}

view.delete_logo = function (page_id,ids,id_logo){
	var status;
		switch(page_id){
		case '1':
			status = 'logo_home';
		break;
		case '2':
			status = 'logo_menu';
		break;
		case '3':
			status = 'logo_collection';
		break;
		case '4':
			status = 'logo_promotion';
		break;
		case '6':
			status = 'logo_contact';
		break;
		case '7':
			status = 'logo_banner';
		break;
		case '8':
			status = 'logo_header';
		break;
	}
	Ext.Ajax.request({
		url:  'services/logo.php',
		method: 'POST',
		params: {act : 'delete_logo',
			     ids : ids,
			     page_id: page_id},
		success: function ( result, request ) {	
			if (result.responseText == 1){
				alert("Xóa thành công");
				view.view_logo(status, id_logo);
			}
		},
		failure: function ( result, request ) {
		}
	});
}
view.view_select_content=function(status,id){
	Ext.Ajax.request({
		url:  'services/view.php',
		method: 'POST',
		params: {act : status},
		success: function ( result, request ) {	
			var data = result.responseText.trim();
			if (data != "") {
				arr =data.split('}#');
				for(var i=0;i<arr.length;i++){
					arr[i]=arr[i].replace('{','');
					arr[i]=arr[i].replace('}','');
					arr[i]=arr[i].split('#');
				}
				var viewtable="	<table class ='view-table' border=1px solid #CCC style='text-align:center;'>" +
							"<th>STT</th><th>Ngày</th><th>Nội&nbsp;Dung</th><th>Hiển&nbsp;Thị</th>";
				for(var i =0 ; i<arr.length ; i++){
					viewtable+= "<tr>";				
					viewtable+= "<td>"+(i+1)+ "</td>";
					viewtable+= "<td>"+ arr[i][2] + "</td>";
					viewtable+= "<td style='text-align:justify;'>"+ arr[i][3] + "</td>";				
					if (arr[i][4]== '1'){
						viewtable+= "<td><input type='radio' name='"+status+"' checked='true' value='"+ arr[i][0] + "'/></td>";
					}else{
						viewtable+= "<td><input type='radio' name='"+status+"' value='"+ arr[i][0] + "'/></td>";
					}
					viewtable+= "</tr>";
				}
				viewtable +="</table>";
				$('#'+id).html(viewtable);
			}else{
				var viewtable =  "<p style='color: Blue;line-height:15px; top:16px; font-size:20px; font-family:tahoma; text-align:center;'>" + 
				"Không tồn tại dữ liệu</p>";
				$('#'+id).html(viewtable);				
			}
		},
		failure: function ( result, request ) {
    	}
	});
}
view.view_select_logo = function(status,id){
	
	Ext.Ajax.request({
		url:  'services/view_logo.php',
		method: 'POST',
		params: {act : status},
		success: function ( result, request ) {	
			var data = result.responseText.trim();
			if (data != "") {
				var location;
				var str="";
				arr =data.split('}#');
				for(var i=0;i<arr.length;i++){
					arr[i]=arr[i].replace('{','');
					arr[i]=arr[i].replace('}','');
					arr[i]=arr[i].split('#');
				}
				var viewtable="	<table class ='view-table' border=1px solid #CCC style='text-align:center;'>" +
							"<th>STT</th><th>Ngày</th><th>Vị&nbsp;Trí</th><th>Hình&nbsp;Ảnh</th><th>Chọn</th>";
	
				for(var i =0 ; i<arr.length ; i++){
					viewtable+= "<tr>";				
					viewtable+= "<td>"+(i+1)+ "</td>";
					viewtable+= "<td>"+ arr[i][2] + "</td>";
					if (arr[i][3] ==1){
						location ="RIGHT";
						str = "id='r_"+status+"'name='r_"+status +"'";
					}
					else{
						location = "LEFT";
						str = "id='l_"+status+"'name='l_"+status +"'";
					}
					viewtable+= "<td>"+ location + "</td>";
					viewtable+= "<td><img src='../"+ arr[i][4] +"' height='50' width='50' /></td>";
					if (arr[i][5]==1){
						viewtable+= "<td><input type='radio'"+ str +"checked='true' value='"+ arr[i][0] + "'/></td>";
					}else{
						viewtable+= "<td><input type='radio'" + str + "value='"+ arr[i][0] + "'/></td>";
					}
					viewtable+= "</tr>";
				}
				viewtable +="</table>";
				$('#'+id).html(viewtable);
			}else{
				var viewtable =  "<p style='color: Blue;line-height:15px; top:16px; font-size:20px; font-family:tahoma; text-align:center;'>" + 
				"Không tồn tại dữ liệu</p>";
				$('#'+id).html(viewtable);				
			}
		},
		failure: function ( result, request ) {
    	}
	});
}

view.select_content = function(page_id,id){
	var status;
	switch(page_id){
		case '1':
			status = 'content_home';
		break;
		case '2':
			status = 'content_menu';
		break;
		case '3':
			status = 'content_collection';
		break;
		case '4':
			status = 'content_promotion';
		break;
		case '6':
			status = 'content_contact';
		break;
	}

	var content_id = $('input:radio[name='+ status +']:checked').val();
	if (content_id){
		Ext.Ajax.request({
			url:  'services/content.php',
			method: 'POST',
			params: {act:'select_content',
					 page_id: page_id,
					 id: content_id
				     },
			success: function ( result, request ) {	
				if (result.responseText == 1){
					view.view_select_content(status,id);
				}
			},
			failure: function ( result, request ) {
			}
		});
	}
}

view.select_logo = function(page_id,id){
	var status;
	switch(page_id){
		case '1':
			status = 'logo_home';
		break;
		case '2':
			status = 'logo_menu';
		break;
		break;
		case '4':
			status = 'logo_promotion';
		break;
		case '6':
			status = 'logo_contact';
		break;
		case '7':
			status = 'logo_banner';
		break;
		case '8':
			status = 'logo_header';
		break;
	}
	var r_id = $('input:radio[name=r_'+ status +']:checked').val();
	var l_id = $('input:radio[name=l_'+ status +']:checked').val();
	var ids="";
	if (r_id){
		ids += r_id + ","
	}
	if(l_id){
		ids += l_id;
	}
	if (ids != ""){
		Ext.Ajax.request({
			url:  'services/logo.php',
			method: 'POST',
			params: {act :'select_logo',
				     ids : ids,
				     page_id: page_id},
			success: function ( result, request ) {	
				if (result.responseText == 1){
					view.view_select_logo(status,id);
				}
			},
			failure: function ( result, request ) {
			}
		});
	}
}
view.list_event = function(){
	Ext.Ajax.request({
		url:  'services/event.php',
		method: 'POST',
		params: {act :'content_event'},
		success: function ( result, request ) {
			var data = result.responseText.trim();
			if (data != ""){
				var arr = arr =data.split('}#');
				for(var i=0;i<arr.length;i++){
					arr[i]=arr[i].replace('{','');
					arr[i]=arr[i].replace('}','');
					arr[i]=arr[i].split('#');
				}
				var list_event = "<select id='list_time' >";
				
				for(var i =0 ; i<arr.length ; i++){
					var value ="";
					switch(arr[i][2]){
						case '0':
							value = arr[i][1].replace(/\-/g,"/") + " - Sáng";
						break;
						case '1':
							value = arr[i][1].replace(/\-/g,"/") + " - Trưa";
						break;
						case '2':
							value = arr[i][1].replace(/\-/g,"/") + " - Chiều";
						break;
						case '3':
							value = arr[i][1].replace(/\-/g,"/") + " - Tối";
						break;
					}
					list_event += "<option value='"+ arr[i][0] +"'>" + value;
					list_event += "</option>";
				}
				list_event +="</select>";
				$('#list_event').html(list_event);
				var val = $('#list_time').attr('value');
				view.add_image('image_event',val);
			}else{
				var list_event = "<p>Vui lòng tạo Event trước khi sử dụng trang này!</p>"
					list_event += "<a href='?act=add_event'>Add Event</a>"
				$('#list_event').html(list_event);
				
			}
		},
		failure: function ( result, request ) {
		}
	});
}
view._checkFileUpload = function(arg){
    if (!common.isImageFile(arg.val())) {
        arg.addClass="input-error";
		arg.val("");
        return false;
    }
	arg.removeClass("input-error");
    return true;
}
view.startCallback = function(status){
    // make something useful before submit (onStart)
    return true;
}
view.completeCallback = function(response){
 try {
 	 if(response != ""){
	 	window.location='/admin/?act='+ response ;
 	 }
 } 
 catch (ex) {
     alert("ERROR: (site.create.completeCallback) : " + ex);
 }
}

view.btnUploadImage = function(status){
	var ids = common.getChecked(status);
	if(ids ==""){
		alert("Chưa chọn hình cần đổi !");
		return false;
	}
    if ($("#file_upload").val()) {
		 $("#file_upload").removeClass("input-error");
    }
    else {
		$("#file_upload").addClass("input-error");
        return false;		
    }
	return true;
}
view.update_click = function(status,ids){
	$("#btnupdate,#btnedit,#btncancel").toggle();
	$("#btndel,#btncheck,#btnuncheck").removeAttr('disabled');
	var list = document.getElementsByName(status);
	$(list).removeAttr('disabled');
	list_id = ids.split(",");
	for(var i=0 ; i< list_id.length;i++){
		$('#tb_' + list_id[i]).attr('disabled',true);
		$('#tb_' + list_id[i]).css('border','');
	}
	common.setUnCheckAll(status);
	if($('#btnuncheck').css('display') != 'none'){
		$('#btncheck,#btnuncheck').toggle();
	}
	var comment = "";
	for(var i=0;i<list_id.length;i++){
		if (i < list_id.length -1){
			comment += $('#tb_' + list_id[i]).val().trim() + "#";
		}else{
			comment += $('#tb_' + list_id[i]).val().trim();
		}
	}
	Ext.Ajax.request({
		url:  'services/add_comment.php',
		method: 'POST',
		params: {act : status,
				 ids : ids,
				 comment : comment},
		success: function ( result, request ) {
			var data = result.responseText.trim();
			view.add_image(status);
		}
	});
}

view.index_click =function (status,ids){
	var list = common.getValueAll('txt_index');
	
	var ids = common.getValueAll(status + "[]");
	alert(ids);
}
